package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactObject;


public class ContactHelper extends HelperWithWebDriverBase {

	private Set<ContactObject> contactCache;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}	

	public Set<ContactObject> getContacts() {
		if (contactCache == null) {
			buildContactCache();
		}
		return contactCache;
	}

	private void buildContactCache() {
		Set<ContactObject> tempSet = new HashSet<ContactObject>();
		manager.getNavigationHelper().openMainPage();
		WebElement form = findForm();
		List<WebElement> checkboxes = form.findElements(By.name(manager.getProperty("checkboxId")));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute(manager.getProperty("checkboxTitle"));
			Pattern p = Pattern.compile(manager.getProperty("contactCheckboxPattern"));
			Matcher m = p.matcher(title);
			if (m.matches()) {
				title = m.group(1);
			}
			ContactObject contact = new ContactObject()
				.setFirstName(title)
				.setId(checkbox.getAttribute(manager.getProperty("checkboxValue")));
			tempSet.add(contact);
		} 
		contactCache = tempSet;
	}

	private WebElement findForm() {
		return findElement(By.id(manager.getProperty("tableId")));
	}

	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().goToContactListPage();
		fillContactForm(contactObject);
		submitContactCreation();
		contactCache = null;
	}

	private void submitContactCreation() {
		click(By.name(manager.getProperty("sumbitButton")));
	}
	
	public void modifyContact(int i, ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		editContactWithNumber(i);
		fillContactForm(contactObject);
		submitModifyContact();
		contactCache = null;
	}
	
	private void editContactWithNumber(int i) {
		click(By.xpath(manager.getProperty("editContactInTableNumber")));
	}

	private void submitModifyContact() {
		click(By.xpath(manager.getProperty("modifyContactButton")));
	}

	public void deleteContact(int i) {
		manager.getNavigationHelper().openMainPage();
		editContactWithNumber(i);
		submitDeleteContact();
		contactCache = null;
	}

	private void submitDeleteContact() {
		click(By.xpath(manager.getProperty("deleteContactButton")));
	}

	private void fillContactForm(ContactObject groupObject) {
		type("firstname", groupObject.firstname);
		type("lastname", groupObject.lastname);
		type("address", groupObject.address);
		type("home", groupObject.home);
		type("mobile", groupObject.mobile);
		type("work", groupObject.work);
		type("email", groupObject.email);
		type("email2", groupObject.email2);
		select("bday", groupObject.bday);
		select("bmonth", groupObject.bmonth);
		type("byear", groupObject.byear);
		type("address2", groupObject.address2);
		type("phone2", groupObject.phone2);
	}

	public ContactObject getContactWithNumber(int i) {
		manager.getNavigationHelper().openMainPage();
		String title = findContactWithNumber(i).getAttribute(manager.getProperty("checkboxTitle"));
		Pattern p = Pattern.compile(manager.getProperty("contactCheckboxPattern"));
		Matcher m = p.matcher(title);
		if (m.matches()) {
			title = m.group(1);
		}
		ContactObject contact = new ContactObject()
			.setFirstName(title)
			.setId(findContactWithNumber(i).getAttribute(manager.getProperty("checkboxValue")));
		return contact;
	}

	private WebElement findContactWithNumber(int i) {
		return findElement(By.xpath(manager.getProperty("contactTableCheckboxNumber")));
	}
}